package com.hongxin.nw.netty.utils.pools;

import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 通道对应设备编码 列表操作
 */
@Slf4j
public class CodePools {
    //通道ID 对应 设备编号
    private final static Map<String, String> codeList = new ConcurrentHashMap<>();

    public static int getSize(){
        return codeList.size();
    }
    public static String getCode(final String cid) throws Exception{
        if (codeList.containsKey(cid)){
            return codeList.get(cid);
        }else{
            throw new Exception("通道找不到: " + cid);
        }
    }
    public static void addCode(final String cid, final String code) {
        codeList.put(cid, code);
    }
    public static void removeCode(final String cid) {
        if (codeList.containsKey(cid)){
            codeList.remove(cid);
        }
    }
    public static void printCtx(){
        Collection<String> values = codeList.values();
        log.info("当前有 {} 台在线设备", getSize());
        for (String code : values) {
            log.info(" - " + code + " - " );
        }
    }

}